package java学习.算法.二叉树;

/**
 * **************
 * 项目名称: 蓝桥杯 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/5/7 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
public class Is平衡二叉树 {
    class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }
    }
    class IsTree{
        boolean Istree;
        int hight;
        TreeNode t;
        public IsTree( boolean Istree,int hight){
            this.hight = hight;
            this.Istree = Istree;
        }
    }
    public boolean IsBalanced_Solution(TreeNode root) {
        IsTree result = TestIsTree(root);
        return result.Istree;
    }

    private IsTree TestIsTree(TreeNode root) {
        if (root==null){
            return new IsTree(true,0);
        }
        boolean bool = false;
        IsTree left = TestIsTree(root.left);
        IsTree right = TestIsTree(root.right);
        int hight = Math.max(left.hight,right.hight);

        if ((left.Istree||left==null)
                &&(right.Istree||right==null)
        &&(Math.abs(left.hight-right.hight)<=1)){
            bool = true;
        }
        return  new IsTree(bool,hight+1);

    }
}
